package ext.tianma.bom.workflow;

import java.util.ArrayList;
import java.util.List;

import wt.doc.WTDocument;
import wt.fc.ObjectReference;
import wt.fc.Persistable;
import wt.fc.collections.WTArrayList;
import wt.part.WTPart;
import wt.util.WTException;
import wt.workflow.work.WfAssignedActivity;
import wt.workflow.work.WorkItem;

import com.ptc.jca.mvc.components.JcaComponentParams;
import com.ptc.mvc.components.AbstractComponentBuilder;
import com.ptc.mvc.components.ColumnConfig;
import com.ptc.mvc.components.ComponentBuilder;
import com.ptc.mvc.components.ComponentConfig;
import com.ptc.mvc.components.ComponentConfigFactory;
import com.ptc.mvc.components.ComponentParams;
import com.ptc.mvc.components.TableConfig;
import com.ptc.mvc.util.ClientMessageSource;

import ext.com.core.CoreUtil;
import ext.tianma.supplier.SupplierInfoQueryBusiness;
import ext.tianma.version.ComToolUtil;



/**Description:不符合要求列表的Table
 * @author    :  YangWen
 * @create at :  2013-8-23   上午10:53:54
 */
@ComponentBuilder("ext.tianma.bom.workflow.SampleApproveTableBuilder")
public class SampleApproveTableBuilder extends AbstractComponentBuilder {
	

	/**
	 * 展示是否输出log信息
	 */
	private static boolean isShow = true;

	private ClientMessageSource msgSource;
	
	private static String dataUtilityId="sampleApproveDataUtilityId" ;
	
	public SampleApproveTableBuilder() {		
		this.msgSource = getMessageSource("ext.tianma.bom.workflow.SampleApproveRB");
	}	
	
	
	/**输出log信息
	 * @param info
	 */
	private static void out(String info){
		if(isShow)
			System.out.println("不符合要求列表的Table:"+SampleApproveTableBuilder.class.getName()+":"+info);
	}
	

	@Override
	public Object buildComponentData(ComponentConfig arg0, ComponentParams componentparams) throws Exception {
		
		String partOid = (String) ((JcaComponentParams) componentparams).getAttribute("partOid");
		
		String workItemOid = (String) ((JcaComponentParams) componentparams).getAttribute("workItemOid");
		if(partOid == null){
		    partOid = (String) componentparams.getParameter("partOid");
		}
		
		if( workItemOid == null ){
			workItemOid =(String)componentparams.getParameter("workItemOid");
		}
		System.out.println("WTPart oid="+partOid);
		
		WTArrayList  wtList = getTableData(partOid , workItemOid);
		List<WTPart> result = new ArrayList<WTPart>();
		
		if( wtList != null ){
			
			for (int i = 0; i < wtList.size(); i++) {
				Object  object = wtList.get(i);
				
				if( object != null ){
					
					if( object instanceof ObjectReference ){
						ObjectReference orf = ( ObjectReference )object;
						object = orf.getObject();
					}
					
					if( object instanceof WTPart ){
						WTPart part = ( WTPart )object;
						result.add(part);
					}
				}
			}
		}
		return result;
	}

	/**YW
	 * 获得table里展示的数据
	 * @param partOid pbo对象的oid值
	 * @return ArrayList<WTPart>
	 * @throws WTException 
	 */
	@SuppressWarnings("deprecation")
	public static WTArrayList getTableData(String partOid , String workItemOid) throws WTException {	  
		WTArrayList result = new WTArrayList();
		
    	if( ComToolUtil.isEmpty( partOid ) ){
    		return result;	
    	}
    	
    	Persistable  persistable =  CoreUtil.getWTObjectByOid( workItemOid );
    	
    	Persistable partTable = SupplierInfoQueryBusiness.getPersistableFromOid(partOid);	
    	if( persistable != null ){
    		WorkItem workItem = ( WorkItem )persistable;
    		WfAssignedActivity wfAssignedActivity = (WfAssignedActivity) workItem.getSource().getObject();
    		
    		if( wfAssignedActivity != null ){
    			//从系统中获取不符合列表集合
    			 WTArrayList inconformityReviewObjs = (WTArrayList) ( wfAssignedActivity.getContext().getValue( "inconformityReviewObjs" ) );
    			 
    			 if( inconformityReviewObjs == null ||  inconformityReviewObjs.size() == 0 ){
    				 
    				 if( partTable instanceof WTPart){
    					 ReWriteBOMReviewConfirmBusiness.showPageData((WTPart)partTable , null , result);
    				 }else if( partTable instanceof WTDocument ){
    					 ReWriteConversionThroughputBusiness.showPageData((WTDocument)partTable , null , result);
    				 }	 
    			 }else{
    				 result = inconformityReviewObjs;
    				 //getWTArrayList( inconformityReviewObjs , result );
    			 }
    		}
    	}
    	
//    	Persistable persistable = SupplierInfoQueryBusiness.getPersistableFromOid(partOid);		
//		if(persistable == null)
//			return null;			
//		out("传入的【对象】是："+persistable.getIdentity());
//		
//		
//		
//		if(persistable instanceof WTPart){			
//			//BOMReviewConfirmBusiness.showPageData((WTPart)persistable , null , result);	
//			ReWriteBOMReviewConfirmBusiness.showPageData((WTPart)persistable , null , result);
//		}else if(persistable instanceof WTDocument){
//			//ConversionThroughputBusiness.showPageData((WTDocument)persistable , null , result);	
//			ReWriteConversionThroughputBusiness.showPageData((WTDocument)persistable , null , result);
//		}	
		
		return result;
	}
	
	
	/**
	 * 往不符合列表中添加数据
	 * @param inconformityReviewObjs
	 * @param result
	 */
	private static void getWTArrayList( WTArrayList inconformityReviewObjs , ArrayList<WTPart> result ) {
		
		if( inconformityReviewObjs == null ){
			return ;
		}
		
		if( result == null ){
			result = new ArrayList<WTPart>();
		}
		
		for ( int i = 0; i < inconformityReviewObjs.size(); i++ ) {
			Object object = inconformityReviewObjs.get(i);
			
			if( object != null ){
				
				if( object instanceof ObjectReference ){
					ObjectReference orf = ( ObjectReference )object;
					object = orf.getObject();
				}
				
				if( object instanceof WTPart ){
					WTPart wtPart = ( WTPart )object;
					result.add(wtPart);
				}
			}
		}	
	}


	@Override
	public ComponentConfig buildComponentConfig(ComponentParams arg0) throws WTException {
		
		ComponentConfigFactory factory = getComponentConfigFactory();
		
		TableConfig result = factory.newTableConfig();
		
		result.setConfigurable(false);
		//result.setActionModel("folderbrowser_toolbar_exportlisttofile_submenu");
		result.setType("wt.fc.WTObject");
		result.setLabel(msgSource.getMessage("sampleWorkFlow"));
		//result.setSelectable(true);
						
		ColumnConfig partNumber = factory.newColumnConfig("number", true);
		partNumber.setLabel(msgSource.getMessage("partNumber"));
		result.addComponent(partNumber);		
				
		ColumnConfig partName = factory.newColumnConfig("name", true);
		partName.setLabel(msgSource.getMessage("partName"));
		result.addComponent(partName);
		
//		ColumnConfig partView = factory.newColumnConfig( "view" , true );
//		partView.setLabel(msgSource.getMessage("partView"));
//		result.addComponent(partView);
		
		ColumnConfig partVersion = factory.newColumnConfig( "versionInfo.identifier.versionId" , true );
		partVersion.setLabel(msgSource.getMessage("partVersion"));
		result.addComponent(partVersion);
								
		ColumnConfig stateState = factory.newColumnConfig("state.state", true);
		stateState.setLabel(msgSource.getMessage("stateState"));
		result.addComponent(stateState);
		
		ColumnConfig partRecognize = factory.newColumnConfig("partRecognize", true);
		partRecognize.setLabel(msgSource.getMessage("partRecognize"));
		partRecognize.setDataUtilityId(dataUtilityId) ;
		result.addComponent(partRecognize);
		
		ColumnConfig partWhetherPartRecognize = factory.newColumnConfig("partWhetherPartRecognize", true);
		partWhetherPartRecognize.setLabel(msgSource.getMessage("partWhetherPartRecognize"));
		partWhetherPartRecognize.setDataUtilityId("SampleApproveWhetherPartRecognizeDataUtilityId") ;
		result.addComponent(partWhetherPartRecognize);
									
		return result;		
		
	}
	
	

}
